package com.gt.common;

import java.util.*;

/**
 * @author wangdong
 * @since 2022-09-05
 */
public class CountAgeUtil {
    public static Map<String, Object> countAge(String from) {

        Map<String, Object> ageMap = new LinkedHashMap<>();
        String age = "";

        int year = 0;

        int month = 0;

        int day = 0;

        from = from.substring(0, 10).replace("-", "");

        int birthYear = Integer.parseInt(from.substring(0, 4));

        int birthMonth = Integer.parseInt(from.substring(4, 6)) - 1;

        int birthDay = Integer.parseInt(from.substring(6, 8));

        Calendar birthday = new GregorianCalendar(birthYear, birthMonth, birthDay);

        if (from == null) {

            age = age + 0;

        } else {

            Calendar c1 = new GregorianCalendar(birthYear, birthMonth, birthDay);

            Calendar c2 = Calendar.getInstance();

            c2.setTime(new Date());

            if (c1.after(c2)) {

                throw new IllegalArgumentException("生日不能超过当前日期");

            }

            int from_year = c1.get(Calendar.YEAR);

            int from_month = c1.get(Calendar.MONTH) + 1;

            int from_day = c1.get(Calendar.DAY_OF_MONTH);

            int MaxDayOfMonth = c1.getActualMaximum(Calendar.DAY_OF_MONTH);

            int to_year = c2.get(Calendar.YEAR);

            int to_month = c2.get(Calendar.MONTH) + 1;

            int to_day = c2.get(Calendar.DAY_OF_MONTH);

            year = to_year - from_year;

            if (to_month - from_month > 0) {

                month = to_month - from_month;

                if (to_day - from_day < 0) {

                    month = month - 1;

                    day = to_day - from_day + MaxDayOfMonth;

                } else {

                    day = to_day - from_day;

                }

            } else if (to_month - from_month == 0) {

                if (to_day - from_day < 0) {

                    --year;

                    month = 11;

                    day = to_day - from_day + MaxDayOfMonth;

                } else {

                    day = to_day - from_day;

                }

            } else if (to_month - from_month < 0) {

                --year;

                month = to_month - from_month + 12;

                if (to_day - from_day < 0) {

                    month = month - 1;

                    day = to_day - from_day + MaxDayOfMonth;

                } else {

                    day = to_day - from_day;

                }

            }

            ageMap.put("year", year);
            ageMap.put("month", month);
            ageMap.put("day", day);
            return ageMap;

        }

        return ageMap;

    }

    public static String syt(String birthday) {
        String patage;

        String birthdate = DateUtils.dateBirthdayFormat(birthday);
        Map<String, Object> ageMap = countAge(birthdate);
        if (Integer.parseInt(ageMap.get("year").toString()) > 5) {
            patage = ageMap.get("year") + "岁";
            return patage;
        } else if (Integer.parseInt(ageMap.get("year").toString()) < 5
                && Integer.parseInt(ageMap.get("year").toString()) >= 1) {
            patage = ageMap.get("year") + "岁" + ageMap.get("month") + "月";
            return patage;
        } else if (Integer.parseInt(ageMap.get("year").toString()) < 1
                && Integer.parseInt(ageMap.get("month").toString()) >= 1) {
            patage = ageMap.get("month") + "月" + ageMap.get("day") + "天";
            return patage;
        } else if (Integer.parseInt(ageMap.get("year").toString()) < 1
                && Integer.parseInt(ageMap.get("month").toString()) < 1) {
            if (Integer.parseInt(ageMap.get("day").toString()) == 0) {
                return "1天";
            }
            patage = ageMap.get("day") + "天";
            return patage;
        }
        return ageMap.get("year") + "岁" + ageMap.get("month") + "月" + ageMap.get("day") + "天";


    }



}
